﻿@inject DrawerService DrawerService

<Input @bind-Value="@value" />
<br/><br />
<Button OnClick="OpenTemplate" Type="primary">Use Template</Button>
<Button OnClick="OpenComponent" Type="primary">Use Component</Button>

@code{

    private string value = "blazor";

    private DrawerRef _drawerRef;

    private async Task OpenTemplate()
    {
        RenderFragment content = @<div>value: @value <br/><Button OnClick="CloseDrawer">close</Button></div>;

        var options = new DrawerOptions()
        {
            Title = "Drawer",
            Width = 350,
            Content = content,
        };

        _drawerRef = await DrawerService.CreateAsync(options);

        _drawerRef.OnClose = () =>
        {
            Console.WriteLine("Close Drawer");
            return Task.CompletedTask;
        };
    }

    private async Task CloseDrawer()
    {
        if (_drawerRef != null)
        {
            await _drawerRef.CloseAsync();
        }
    }

    private async Task OpenComponent()
    {
        var options = new DrawerOptions()
        {
            Title = "Component",
            Width = 350,
        };

        var drawerRef = await DrawerService.CreateAsync<DrawerTemplateDemo, string, string>(options, value);

        drawerRef.OnOpen = () =>
        {
            Console.WriteLine("OnAfterOpen");
            return Task.CompletedTask;
        };

        drawerRef.OnClose = async result =>
        {
            Console.WriteLine("OnAfterClose:" + result);
            value = result;
            await InvokeAsync(StateHasChanged);
        };

    }

}